#导包
import json
from pyecharts.charts import Map
from pyecharts.options import VisualMapOpts,TitleOpts
import norm
#读取数据
#file = open("D:/me/study/python/execise/开发实例/数据可视化/地图案例/data/疫情.txt",'r',encoding="UTF-8")
file = open("C:/Users/tanglx/Documents/me/study/python/execise/开发实例/数据可视化/地图案例/data/疫情.txt",'r',encoding="UTF-8")
file_dict = file.read()
file.close()
#将json数据转为python数据
file_dict=json.loads(file_dict)
#取出源数据中的children字典(保存了全省数据)
whole_province_data=file_dict["areaTree"][0]["children"]
#返回省map数据
def epidemic(i):
    '''
    :param i: whole_province_data中各省的下标
    :return: 返回各省map数据
    '''
    # 创建一个空列表
    epidemic_data = []
    for countries_data in whole_province_data[i]["children"]:
        epidemic_data.append((countries_data["name"]+"市", countries_data["total"]["confirm"]))
    print(whole_province_data[i]["name"],i)
    print(epidemic_data)
    return epidemic_data

#构建省地图
map = Map()
map.add("河南省疫情分布",epidemic(3),"河南")
# 设置全局配置，定制分段的视觉映射
map.set_global_opts(
    title_opts=TitleOpts(title="全国疫情地图"), #显示大标题
    visualmap_opts=VisualMapOpts(
        is_show=True,           # 是否显示
        is_piecewise=True,      # 是否分段
        pieces=[
            {"min": 1, "max": 99, "lable": "1~99人", "color": "#CCFFFF"},
            {"min": 100, "max": 999, "lable": "100~9999人", "color": "#FFFF99"},
            {"min": 1000, "max": 4999, "lable": "1000~4999人", "color": "#FF9966"},
            {"min": 5000, "max": 9999, "lable": "5000~99999人", "color": "#FF6666"},
            {"min": 10000, "max": 99999, "lable": "10000~99999人", "color": "#CC3333"},
            {"min": 100000, "lable": "100000+", "color": "#990033"},
        ]
    )
)
map.render("河南疫情分布.html")